package com.xci.xajtfb._core;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.github.lvyanyang.configuration.CoreAutoConfiguration;
import com.github.lvyanyang.service.CoreService;
import com.xci.xajtfb.bus.api.TbBusLineApiController;
import com.xci.xajtfb.gjzn.api.GjznApiController;
import com.xci.xajtfb.om.api.BasicCustomerApiController;
import com.xci.xajtfb.wxc.api.SchoolApiController;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import springfox.documentation.spring.web.plugins.Docket;

import javax.annotation.Resource;

/**
 * 交通发布模块自动配置
 * @author 吕艳阳 18049047588@qq.com
 * @since 2021-02-04 21:55
 */
@Configuration
@EnableConfigurationProperties({SmsProperties.class, WxMaProperties.class})
@AutoConfigureAfter(CoreAutoConfiguration.class)
public class XajtfbAutoConfiguration {
    @Resource private CoreService coreService;
    @Resource private CacheManager cacheManager;
    @Resource private RestTemplateBuilder builder;
    @Resource private WxMaProperties wxMaProperties;

    @Bean
    public Cache phoneCaptchaCache() {
        return cacheManager.getCache("phoneCaptchaCache");
    }

    @Bean
    public Cache phoneDayTimesLimitCache() {
        return cacheManager.getCache("phoneDayTimesLimitCache");
    }

    // 使用RestTemplateBuilder来实例化RestTemplate对象，spring默认已经注入了RestTemplateBuilder实例
    @Bean
    public RestTemplate restTemplate() {
        return builder.build();
    }

    @Bean
    public WxMaService wxMaService() {
        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        config.setAppid(wxMaProperties.getAppid());
        config.setSecret(wxMaProperties.getSecret());
        config.setToken(wxMaProperties.getToken());
        config.setAesKey(wxMaProperties.getAesKey());
        config.setMsgDataFormat(wxMaProperties.getMsgDataFormat());

        WxMaService service = new WxMaServiceImpl();
        service.setWxMaConfig(config);
        return service;
    }

    @Bean
    public Docket omModuleApiDocket() {
        return coreService.buildApiDocket("收费系统模块", BasicCustomerApiController.class.getPackage().getName());
    }

    @Bean
    public Docket busModuleApiDocket() {
        return coreService.buildApiDocket("公交模块", TbBusLineApiController.class.getPackage().getName());
    }

    @Bean
    public Docket wxcModuleApiDocket() {
        return coreService.buildApiDocket("维修处模块", SchoolApiController.class.getPackage().getName());
    }

    @Bean
    public Docket wxcGjznApiDocket() {
        return coreService.buildApiDocket("公交智能调模块", GjznApiController.class.getPackage().getName());
    }

}
